package com.zenithmind.common.utils;

import com.zenithmind.common.exception.BusinessException;

import java.util.Collection;
import java.util.regex.Pattern;

/**
 * 参数验证工具类 - 遵循单一职责原则
 * 只负责参数验证相关的工具方法
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
public class ValidationUtils {

    private static final Pattern EMAIL_PATTERN = Pattern.compile(
            "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$");
    
    private static final Pattern PHONE_PATTERN = Pattern.compile(
            "^1[3-9]\\d{9}$");
    
    private static final Pattern ID_PATTERN = Pattern.compile(
            "^[a-zA-Z0-9_-]{1,64}$");

    /**
     * 验证字符串不为空
     * 
     * @param value 待验证的值
     * @param fieldName 字段名
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateNotBlank(String value, String fieldName) {
        if (value == null || value.trim().isEmpty()) {
            throw new BusinessException(fieldName + "不能为空");
        }
    }

    /**
     * 验证字符串长度
     * 
     * @param value 待验证的值
     * @param fieldName 字段名
     * @param maxLength 最大长度
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateLength(String value, String fieldName, int maxLength) {
        if (value != null && value.length() > maxLength) {
            throw new BusinessException(fieldName + "长度不能超过" + maxLength + "个字符");
        }
    }

    /**
     * 验证字符串长度范围
     * 
     * @param value 待验证的值
     * @param fieldName 字段名
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateLength(String value, String fieldName, int minLength, int maxLength) {
        if (value != null) {
            if (value.length() < minLength) {
                throw new BusinessException(fieldName + "长度不能少于" + minLength + "个字符");
            }
            if (value.length() > maxLength) {
                throw new BusinessException(fieldName + "长度不能超过" + maxLength + "个字符");
            }
        }
    }

    /**
     * 验证数值范围
     * 
     * @param value 待验证的值
     * @param fieldName 字段名
     * @param min 最小值
     * @param max 最大值
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateRange(Integer value, String fieldName, int min, int max) {
        if (value != null) {
            if (value < min) {
                throw new BusinessException(fieldName + "不能小于" + min);
            }
            if (value > max) {
                throw new BusinessException(fieldName + "不能大于" + max);
            }
        }
    }

    /**
     * 验证数值范围
     * 
     * @param value 待验证的值
     * @param fieldName 字段名
     * @param min 最小值
     * @param max 最大值
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateRange(Long value, String fieldName, long min, long max) {
        if (value != null) {
            if (value < min) {
                throw new BusinessException(fieldName + "不能小于" + min);
            }
            if (value > max) {
                throw new BusinessException(fieldName + "不能大于" + max);
            }
        }
    }

    /**
     * 验证集合不为空
     * 
     * @param collection 待验证的集合
     * @param fieldName 字段名
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateNotEmpty(Collection<?> collection, String fieldName) {
        if (collection == null || collection.isEmpty()) {
            throw new BusinessException(fieldName + "不能为空");
        }
    }

    /**
     * 验证集合大小
     * 
     * @param collection 待验证的集合
     * @param fieldName 字段名
     * @param maxSize 最大大小
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateSize(Collection<?> collection, String fieldName, int maxSize) {
        if (collection != null && collection.size() > maxSize) {
            throw new BusinessException(fieldName + "数量不能超过" + maxSize);
        }
    }

    /**
     * 验证邮箱格式
     * 
     * @param email 邮箱地址
     * @param fieldName 字段名
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateEmail(String email, String fieldName) {
        if (email != null && !email.trim().isEmpty() && !EMAIL_PATTERN.matcher(email).matches()) {
            throw new BusinessException(fieldName + "格式不正确");
        }
    }

    /**
     * 验证手机号格式
     * 
     * @param phone 手机号
     * @param fieldName 字段名
     * @throws BusinessException 验证失败时抛出
     */
    public static void validatePhone(String phone, String fieldName) {
        if (phone != null && !phone.trim().isEmpty() && !PHONE_PATTERN.matcher(phone).matches()) {
            throw new BusinessException(fieldName + "格式不正确");
        }
    }

    /**
     * 验证ID格式
     * 
     * @param id ID值
     * @param fieldName 字段名
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateId(String id, String fieldName) {
        validateNotBlank(id, fieldName);
        if (!ID_PATTERN.matcher(id).matches()) {
            throw new BusinessException(fieldName + "格式不正确");
        }
    }

    /**
     * 验证枚举值
     * 
     * @param value 待验证的值
     * @param fieldName 字段名
     * @param validValues 有效值数组
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateEnum(Integer value, String fieldName, Integer... validValues) {
        if (value != null) {
            for (Integer validValue : validValues) {
                if (validValue.equals(value)) {
                    return;
                }
            }
            throw new BusinessException(fieldName + "值不正确");
        }
    }

    /**
     * 验证枚举值
     * 
     * @param value 待验证的值
     * @param fieldName 字段名
     * @param validValues 有效值数组
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateEnum(String value, String fieldName, String... validValues) {
        if (value != null && !value.trim().isEmpty()) {
            for (String validValue : validValues) {
                if (validValue.equals(value)) {
                    return;
                }
            }
            throw new BusinessException(fieldName + "值不正确");
        }
    }

    /**
     * 验证文件大小
     * 
     * @param fileSize 文件大小（字节）
     * @param fieldName 字段名
     * @param maxSize 最大大小（字节）
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateFileSize(Long fileSize, String fieldName, long maxSize) {
        if (fileSize != null && fileSize > maxSize) {
            throw new BusinessException(fieldName + "大小不能超过" + formatFileSize(maxSize));
        }
    }

    /**
     * 验证文件类型
     * 
     * @param mimeType MIME类型
     * @param fieldName 字段名
     * @param allowedTypes 允许的类型
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateFileType(String mimeType, String fieldName, String... allowedTypes) {
        if (mimeType != null && !mimeType.trim().isEmpty()) {
            for (String allowedType : allowedTypes) {
                if (mimeType.startsWith(allowedType)) {
                    return;
                }
            }
            throw new BusinessException(fieldName + "类型不支持");
        }
    }

    /**
     * 验证对象不为空
     * 
     * @param object 待验证的对象
     * @param fieldName 字段名
     * @throws BusinessException 验证失败时抛出
     */
    public static void validateNotNull(Object object, String fieldName) {
        if (object == null) {
            throw new BusinessException(fieldName + "不能为空");
        }
    }

    /**
     * 格式化文件大小
     * 
     * @param size 文件大小（字节）
     * @return 格式化后的大小
     */
    private static String formatFileSize(long size) {
        if (size <= 0) return "0 B";
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double fileSize = size;
        while (fileSize >= 1024 && unitIndex < units.length - 1) {
            fileSize /= 1024;
            unitIndex++;
        }
        return String.format("%.1f %s", fileSize, units[unitIndex]);
    }
}
